package com.cw.config.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.extension.service.impl.*;
import com.cw.bo.*;
import com.cw.config.service.*;
import com.cw.mapper.*;
import com.cw.vo.*;
import com.cw.websocket.*;
import java.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.*;
import org.springframework.context.annotation.*;
import org.springframework.scheduling.annotation.*;
import org.springframework.stereotype.*;
import org.springframework.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cc
 * @since 2020-07-15
 */
@Service
public class RemindRecordServiceImpl extends
    ServiceImpl<RemindRecordMapper, RemindRecord> implements IRemindRecordService {

    @Autowired
    @Lazy
    private SocketHandler socketHandler;

    @Override
    public List<RemindRecord> getRemindList(String type, String userName) {
	  QueryWrapper<RemindRecord> wrapper = new QueryWrapper<>();
	  wrapper.eq("USER_NAME", userName);
	  if (StringUtils.isNotBlank(type)) {
		wrapper.eq("IS_READ", type);
	  }
	  wrapper.orderByDesc("CREATE_TIME");
	  return baseMapper.selectList(wrapper);
    }

    @Override
    public List<RemindRecord> getAppRemindList(String type) {
	  QueryWrapper<RemindRecord> wrapper = new QueryWrapper<>();
	  if (StringUtils.isNotBlank(type)) {
		wrapper.eq("IS_READ", type).orderByDesc("CREATE_TIME");
	  }
	  return baseMapper.selectList(wrapper);
    }

    @Override
    public RemindRecord getRemindDetail(String id) {
	  QueryWrapper<RemindRecord> wrapper = new QueryWrapper<>();
	  wrapper.eq("ID", id);
	  return baseMapper.selectOne(wrapper);
    }

    @Override
    public List<RemindCountResponse> getRemindCountResponse(List<String> userNames) {
	  return this.baseMapper.getRemindCount(userNames);
    }

    @Override
    @Async("taskExecutor")
    public void sendMessageAfterLogin(String loginCode) throws InterruptedException {
	  Thread.sleep(2000);
	  List<String> names = new ArrayList<>();
	  names.add(loginCode);
	  //查询要收消息的消息数量
	  List<RemindCountResponse> remindCount = this.baseMapper.getRemindCount(names);
	  //websocket发消息
	  socketHandler.sendMessageToUserLists(remindCount);
    }

    @Override
    @Async("taskExecutor")
    public void sendMessages(List<String> loginCodes) {
	  //查询要收消息的消息数量
	  List<RemindCountResponse> remindCount = this.baseMapper.getRemindCount(loginCodes);
	  //websocket发消息
	  socketHandler.sendMessageToUserLists(remindCount);
    }

    @Override
    public Integer getMsgSumByUserId(String loginCode) {
	  Integer sum = 0;
	  List<String> loginCodes = new ArrayList<>();
	  loginCodes.add(loginCode);
	  List<RemindCountResponse> remindCount = this.baseMapper.getRemindCount(loginCodes);
	  if (!CollectionUtils.isEmpty(remindCount)) {
		RemindCountResponse remindCountResponse = remindCount.get(0);
		if (StringUtils.isNotEmpty(remindCountResponse.getMsgCount())) {
		    sum += Integer.parseInt(remindCountResponse.getMsgCount());
		}
	  }
	  return sum;
    }
}
